local Class = require('lua.lang.Class')
local ConstantsMapper = require('lua.decompiler.virtual.code.mapper.ConstantsMapper')

--- @class DefaultConstantMapper : ConstantsMapper
--- @field constMap table<number, string>
local DefaultConstantMapper = Class {
	__extends = ConstantsMapper
}

DefaultConstantMapper.constMap = {
	[ConstantsMapper.CONST_UPVALUE] = 'upvalue',
	[ConstantsMapper.CONST_END] = 'end',
	[ConstantsMapper.CONST_TRUE] = 'true',
	[ConstantsMapper.CONST_FALSE] = 'false',
	[ConstantsMapper.CONST_LOCAL] = 'local',
	[ConstantsMapper.CONST_NIL] = 'nil',
	[ConstantsMapper.CONST_VARARGS] = '...',
	[ConstantsMapper.CONST_RETURN] = 'return',
	[ConstantsMapper.CONST_FUNCTION] = 'function',
	[ConstantsMapper.CONST_TOP] = 'top',
	[ConstantsMapper.CONST_V] = 'v',
	[ConstantsMapper.CONST_PACK] = 'pack',
	[ConstantsMapper.CONST_UNPACK] = 'unpack',
	[ConstantsMapper.CONST_NV] = 'nv',
	[ConstantsMapper.CONST_FOR] = 'for',
	[ConstantsMapper.CONST_DO] = 'do',
	[ConstantsMapper.CONST_TEMP] = 'temp',
	[ConstantsMapper.CONST_FOR_INIT] = 'for_i',
	[ConstantsMapper.CONST_ENV] = '_ENV',
	[ConstantsMapper.CONST_TABLE] = 'table',
	[ConstantsMapper.CONST_NEW_UPVALUE] = 'newUpvalue',
	[ConstantsMapper.CONST_CLOSE] = 'close',
	[ConstantsMapper.CONST_SETMETATABLE] = 'setmetatable',
	[ConstantsMapper.CONST_IF] = 'if',
	[ConstantsMapper.CONST_THEN] = 'then',
	[ConstantsMapper.CONST_ELSE] = 'else',
	[ConstantsMapper.CONST_SUB_UPVALUES] = 'subUpvalues',
	[ConstantsMapper.CONST_SUB_UPVALUES_N] = 'subUpvaluesN',
	[ConstantsMapper.CONST_TEMP2] = 'temp2',
	[ConstantsMapper.CONST_TEMP3] = 'temp3',
	[ConstantsMapper.CONST_PARAMS] = 'params'
}

--- @return DefaultConstantMapper
function DefaultConstantMapper:new()
	--- @type DefaultConstantMapper
	local o = {}
	return setmetatable(o, DefaultConstantMapper)
end

--- @param i number
--- @return string
function DefaultConstantMapper:get(i)
	return self.constMap[i]
end

--- @param i number
--- @return string
function DefaultConstantMapper:getConstString(i)
	local s = self:get(i)
	assert(s ~= nil, 'invalid constant index: ' .. tostring(i))
	return s
end

return DefaultConstantMapper